package com.vanzo.talkie;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.AppLaunchChecker;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.vanzo.contact.ContactAddActivity;
import com.vanzo.talkie2.R;
import com.vanzo.util.LogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tisen on 2018/1/20.
 */

public class ChannelActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = ChannelActivity.class.getSimpleName();

    private ImageView topBackImg;
    private TextView topTitleText;
    private RecyclerView channelRecycler;
    private ImageView channelAddImg;

    private ChannelAdapter channelAdapter;
    private Long tempId;

    private Application application;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_channel);
        application = (Application) getApplication();
        topBackImg = (ImageView) findViewById(R.id.channel_back_img);
        topTitleText = (TextView) findViewById(R.id.channel_top_title);
        channelRecycler = (RecyclerView) findViewById(R.id.channel_recycler);
        channelAddImg = (ImageView) findViewById(R.id.channel_add_img);
        channelRecycler.setLayoutManager(new LinearLayoutManager(this));
        channelAdapter = new ChannelAdapter(this, application);
        channelAdapter.setOnClickListener(new ChannelAdapter.ItemOnClickListener() {
            @Override
            public void onClick(Long id) {
                menuOption(3, id);
            }

            @Override
            public void onLongClick(Long id) {
                tempId = id;
                if (builder == null) {
                    builder = new AlertDialog.Builder(ChannelActivity.this);
                    builder.setTitle(R.string.menu_dialog_title);
                    builder.setItems(R.array.channel_item_menu, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            menuOption(which, tempId);
                        }
                    });
                }
                builder.create().show();
            }
        });
        channelRecycler.setAdapter(channelAdapter);

        topBackImg.setOnClickListener(this);
        topTitleText.setOnClickListener(this);
        channelAddImg.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        notifyDataSetChanged();
    }

    private void notifyDataSetChanged() {
        channelAdapter.setChannels(application.getChannels());
        setTopTitleText();
    }

    private void setTopTitleText() {
        topTitleText.setText("频道列表(" + application.getSize() + "/20)");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.channel_back_img:
                finish();
                break;
            case R.id.channel_top_title:
                channelRecycler.scrollToPosition(0);
                break;
            case R.id.channel_add_img:
                menuOption(4, null);
                break;
        }
    }

    private AlertDialog.Builder builder;


    private void menuOption(int which, Long id) {
        LogUtil.put(this, "menuOption which = " + which + " | id = " + id,LogUtil.WARN);
        Intent intent = new Intent();
        intent.setClass(this, ChannelEditActivity.class);
        intent.putExtra("id", id);
        switch (which) {
            case 0:
                channelAdapter.setUsed(id);
                application.setUsedId(id);
                setResult(TalkieActivity.CHANNEL_CHANGE_RESULT_CODE);
                finish();
                break;
            case 1:
                intent.setAction(ChannelEditActivity.EDIT_CHANNEL_ACTION);
                startActivity(intent);
                break;
            case 2:
                application.deleteChannel(id);
                notifyDataSetChanged();
                if (id.equals(application.getUsedId())) {
                    setResult(TalkieActivity.CHANNEL_CHANGE_RESULT_CODE);
                    finish();
                }
                break;
            case 3:
                intent.setAction(ChannelEditActivity.VIEW_CHANNEL_ACTION);
                startActivity(intent);
                break;
            case 4:
                if (application.getSize() < 20) {
                    intent.setAction(ChannelEditActivity.NEW_CHANNEL_ACTION);
                    startActivity(intent);
                } else {
                    Toast.makeText(this, "频道列表最多保存20条", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                break;
        }
    }
}
